package com.app.shop.controller.system;

import com.app.shop.util.ApiRest;
import com.app.shop.util.Constant;
import com.app.shop.util.SysModel;
import com.app.shop.entity.SysAuth;
import com.app.shop.entity.SysMenu;
import com.app.shop.entity.SysReAuthMenu;
import com.app.shop.entity.query.SysAuthQuery;
import com.app.shop.entity.query.SysMenuQuery;
import com.app.shop.config.security.DgbSecurityUserHelper;
import com.app.shop.service.system.SysAuthService;
import com.app.shop.service.system.SysMenuService;
import com.app.shop.service.system.SysReAuthMenuService;
import com.app.shop.entity.show.BaseShow;
import com.app.shop.entity.show.SysAuthShow;
import com.app.shop.util.ClassUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 首页数据展示处理Controller模块
 *
 * @author chen li wei
 * @version 21:14 2019-5-10
 */
@Controller
@RequestMapping(value = "/sysAuth")
public class AuthController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(AuthController.class);

    @Autowired
    SysAuthService sysAuthService;

    @Autowired
    SysMenuService sysMenuService;

    @Autowired
    SysReAuthMenuService sysReAuthMenuService;

    /**
     * 查询权限列表
     *
     * @param sysAuthQuery
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/getAuthList")
    public ApiRest getDictList(@RequestBody SysAuthQuery sysAuthQuery) {
        log.debug(JSONObject.toJSON(sysAuthQuery).toString());
        //模拟分页查询
        Page<SysAuth> sysAuthPage = sysAuthService.getSysAuthList(sysAuthQuery);
        //查询结果对象置换
        List<SysAuthShow> resList = new ArrayList<SysAuthShow>();

        SysAuthShow sysAuthShow;
        //数据转换
        for (SysAuth sysAuth : sysAuthPage.getResult()) {
            sysAuthShow = new SysAuthShow();
            try {
                ClassUtil.fatherToChild(sysAuth, sysAuthShow);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //码表值补全
            sysAuthShow.dataSet();
            resList.add(sysAuthShow);

        }
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE,
                new BaseShow(sysAuthPage.getPageNum(), sysAuthPage.getPageSize(), sysAuthPage.getTotal(), sysAuthPage.getPages(), resList));
    }

    /**
     * 权限启用
     *
     * @param sysAuthQuery 需要启用的权限信息
     * @return 需要重置的权限信息
     */
    @ResponseBody
    @RequestMapping(value = "updateAuthStart")
    public ApiRest resetUserStart(@RequestBody SysAuthQuery sysAuthQuery) {
        log.debug(JSONObject.toJSON(sysAuthQuery).toString());
        sysAuthQuery.setOptionUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        sysAuthService.updateAuthStart(sysAuthQuery);
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
    }

    /**
     * 权限禁用
     *
     * @param sysAuthQuery 需要禁用的权限信息
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "updateAuthStop")
    public ApiRest updateUserStop(@RequestBody SysAuthQuery sysAuthQuery) {
        log.debug(JSONObject.toJSON(sysAuthQuery).toString());
        sysAuthQuery.setOptionUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        sysAuthService.updateAuthStop(sysAuthQuery);
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
    }

    /**
     * 权限信息查询
     *
     * @param sysAuthQuery 需要查询的权限信息
     * @return 权限信息
     */
    @ResponseBody
    @RequestMapping(value = "getAuthInfo")
    public ApiRest getAuthInfo(@RequestBody SysAuthQuery sysAuthQuery) {
        log.debug(JSONObject.toJSON(sysAuthQuery).toString());
        SysAuthShow sysAuthShow = sysAuthService.getAuthInfo(sysAuthQuery);
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, sysAuthShow);
    }


    /**
     * 权限信息添加
     *
     * @param sysAuth 需要添加的权限信息
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "addAuthInfo")
    public ApiRest addAuthInfo(@RequestBody SysAuth sysAuth) {
        log.debug(JSONObject.toJSON(sysAuth).toString());
        sysAuth.setCreateUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        sysAuth.setUpdateUserId(sysAuth.getCreateUserId());

        SysModel sysModel = sysAuthService.addAuthInfo(sysAuth);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }

    /**
     * 权限信息修改
     *
     * @param sysAuth 需要修改的权限信息
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "updateAuthInfo")
    public ApiRest updateAuthInfo(@RequestBody SysAuth sysAuth) {
        log.debug(JSONObject.toJSON(sysAuth).toString());
        sysAuth.setUpdateUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        SysModel sysModel = sysAuthService.updateAuthInfo(sysAuth);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }

    /**
     * 根据权限获取菜单列表
     *
     * @param sysAuthQuery
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getDictByAuthId")
    public ApiRest getDictByAuthId(@RequestBody SysAuthQuery sysAuthQuery) {
        log.debug(JSONObject.toJSON(sysAuthQuery).toString());
        //查询所有菜单列表
        List<SysMenu> sysMenuList = sysMenuService.getSysMenuList(new SysMenuQuery());
        //查询权限对于菜单列表
        List<SysReAuthMenu> sysReAuthMenuList = sysReAuthMenuService.getSysMenuListByAuthId(sysAuthQuery.getAuthId());
        Map map = new HashMap();
        map.put("authOptions", sysMenuList);
        map.put("checkedAuths", sysReAuthMenuList);
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, map);
    }

    /**
     * 根据权限ID更新相关权限信息
     *
     * @param sysAuthQuery 需要修改的参数数据
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "updateAuthMenuInfo")
    public ApiRest updateAuthUserInfo(@RequestBody SysAuthQuery sysAuthQuery) {
        log.debug(JSONObject.toJSON(sysAuthQuery).toString());

        sysAuthQuery.setOptionUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        SysModel sysModel = sysReAuthMenuService.updateAuthMenuInfo(sysAuthQuery);

        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }
}
